package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.dto.EquipmentDTO;
import com.woniuxy.factory.entity.Equipment;
import com.woniuxy.factory.entity.EquipmentType;
import com.woniuxy.factory.entity.WorkShop;
import com.woniuxy.factory.mapper.EquipmentMapper;
import com.woniuxy.factory.mapper.EquipmentTypeMapper;
import com.woniuxy.factory.mapper.WorkShopMapper;
import com.woniuxy.factory.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EquipmentServiceImp implements EquipmentService {
    @Autowired
    EquipmentMapper equipmentMapper;
    @Autowired
    WorkShopMapper workShopMapper;
    @Autowired
    EquipmentTypeMapper equipmentTypeMapper;

    @Override
    public List<EquipmentDTO> findEquipmentByCondtion(Equipment equipment, int page, int limit) throws Exception {
        //开始查询的索引
        int index = (page - 1) * limit;
        //条件查询
        List<Equipment> equipments = equipmentMapper.selectEquimentByCondition(equipment, index, limit);
        WorkShop workShop;
        List<EquipmentDTO> equipmentDTOs = new ArrayList<>();
        try {
            //获取其中一个设备的类型id
            //Integer tid = equipments.get(0).getTid();
            //根据id查询设备类型信息
            EquipmentType equipmentType;
            //没有查询到设备
            System.out.println("数量："+equipments.size());
            if (equipments.size()==0){
                throw new Exception("没有该设备，请重新输入");
            }
            for (Equipment eq :
                    equipments) {
                //根据id查询设备类型
                equipmentType= equipmentTypeMapper.selectEquipmentTypeById(eq.getTid());
                //根据id查询车间信息
                System.out.println(eq.getWosId());
                workShop = workShopMapper.selectWorkShopById(eq.getWosId());
                //装配equipmentDTO
                EquipmentDTO equipmentDTO = new EquipmentDTO();
                equipmentDTO.setWorkShop(workShop);
                equipmentDTO.setEquipmentType(equipmentType);
                equipmentDTO.setEquipment(eq);
                //将equipmentDTO存入容器
                equipmentDTOs.add(equipmentDTO);
            }
        } catch (Exception e) {
            //查询结果为空
            throw new Exception("没有该设备，请重新输入");
        }
        return equipmentDTOs;
    }

    @Override
    public Integer totalEquipmentByCondtion(Equipment equipment) {
        return equipmentMapper.totalEquipmentByCondition(equipment);
    }

    @Override
    public void addEquipemt(Equipment equipment) {
        equipmentMapper.insertEquipment(equipment);
    }

    @Override
    public void deletEquipmentByEid(int eid) {
        equipmentMapper.deletEquipmentById(eid);
    }

    @Override
    public void modifyEquipment(Equipment equipment) {
        equipmentMapper.updateEquipment(equipment);
    }
}
